<?php

//
// Functions for Ruffy v2
// Nice & clean ftw
// By danopia
//

/*function send($text)
{
	global $socket;
	fputs($socket, "$text\r\n");
	echo ">>> $text\r\n";
}*/

// Not clean... blame duck
function updateAccess(){
  global $levels;
  $accessFile=fopen("data/access.dat","w");
  fwrite($accessFile,serialize($levels));
  fclose($accessFile);
}

// Not clean... blame duck
function send($cmd, $silent = false)
{
  global $socket,$joined,$antiFlood,$source_nick,$antiFloodGlobal,$floodIgnores,$antifloodswitch;
  $cmd=str_replace("\r","",$cmd);
  $cmd=str_replace("\n","",$cmd);
  if(strlen($cmd)>512){
    $cmd=substr($cmd,0,500)." [abbr]";
  }
  if ($antifloodswitch) {
	  if(!array_key_exists($source_nick,$floodIgnores)){
	    $floodIgnores[$source_nick]=0;
	  }
	  if(!array_key_exists($source_nick,$antiFlood)){
	    $antiFlood[$source_nick]=0;
	  }
	  if($antiFlood[$source_nick]<2 and $antiFloodGlobal<5){
	    @fwrite($socket, $cmd . "\r\n", strlen($cmd) + 2); // sends the command to the server
	    if($silent)
			log_send("(Hidden command)");
		else
			log_send(str_replace(chr(7), '*', $cmd));
	  }
	  if(($joined) && (!$silent)){
	      $antiFlood[$source_nick]++;
	      //$antiFloodGlobal++;
	  }
  } else {
	  @fwrite($socket, $cmd . "\r\n", strlen($cmd) + 2); // sends the command to the server
    if($silent)
		log_send("(Hidden command)");
    else
		log_send(str_replace(chr(7), '*', $cmd));
	}
}

// Yaya clean

function notice($message, $target = '')
{
	global $nickname, $args, $source_nick;
	if ($target == '')
	{
		$target = ($args[2] == $nickname) ? $source_nick : $args[2];
	}
	send("NOTICE $target :$message");	
}
function msg($message, $target = '')
{
	global $nickname, $args, $source_nick;
	if ($target == '')
	{
		$target = ($args[2] == $nickname) ? $source_nick : $args[2];
	}
	send("PRIVMSG $target :$message");
}
function reply($message, $target = '')
{
	global $nickname, $args, $source_nick;
	if ($target == '')
	{
		$target = ($args[2] == $nickname) ? $source_nick : $args[2];
	}
	if ($target[0] == '#')
	{
		send("PRIVMSG $target :$source_nick: $message");
	}
	else
	{
		send("PRIVMSG $target :$message");
	}
}

function ctcp_request($contents, $target = '')
{
	msg(chr(1) . $contents . chr(1), $target);
}
function ctcp_reply($contents, $target = '')
{
	notice(chr(1) . $contents . chr(1), $target);
}
function action($contents, $target = '')
{
	ctcp_request("ACTION $contents", $target);
}

function chan_join($channel, $key = '')
{
	if ($key == '')
	{
		send("JOIN $channel");
	}
	else
	{
		send("JOIN $channel $key");
	}
}
function chan_part($channel, $message = '')
{
	global $def_part;
	if ($message == '')
	{
		$message = $def_part;
	}
	send("PART $channel :$message");
}
function quit($message = '')
{
	global $def_quit;
	if ($message == '')
	{
		$message = $def_quit;
	}
	send("QUIT :$message");
}

function cmd($cmd, $min_level = 0)
{
	global $msg_cmd, $level, $source_nick, $modules, $current_module;
	
	//echo "$msg_cmd && $cmd\r\n";
	
	$modules[$current_module][$cmd] = $min_level;
	
	if ($msg_cmd != $cmd)
		return false;
	
	if ($level >= $min_level)
		return true;

	notice("Error: You are level $level. You need to be $min_level or greater to use the \"$cmd\" command.", $source_nick);
	return false;
}

?>
